package util;

import java.io.*;
import java.util.*;
import java.io.FileWriter;
import java.io.IOException;

import entity.Metrics;



public class MetricsWriter {
	String condition;
	boolean header;
	String outputFileName;
	Collection<Metrics> metricsList;
	
	public MetricsWriter(Collection<Metrics> metricsList){
		condition = null;
		header = true;
		outputFileName = "result.csv";
		this.metricsList = metricsList;
	}
	
	

	
	public String getCondition() {
		return condition;
	}




	public void setCondition(String condition) {
		this.condition = condition;
	}




	public boolean isHeader() {
		return header;
	}




	public void setHeader(boolean header) {
		this.header = header;
	}




	public String getOutputFileName() {
		return outputFileName;
	}




	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}




	public void writeCsv() throws IOException{

		
		File file = new File(outputFileName);
	      // creates the file
	      file.createNewFile();
	      
	      
	      FileWriter writer = new FileWriter(file);
	      
	   // Writes the content to the file
	      
	      for(Metrics m : metricsList){
	    	  String content=m.toCSV();
	    	  if(header == false){
	    		 content = (String) content.subSequence(content.indexOf(",")+1, content.length());
	    	  }
	    	  if(condition == null){
	    		  writer.write(content); 
	    	  }else  if(m.getFullname().contains(condition)){
	    		  writer.write(content); 
	    	  }
	    	  
	      }
	      
	      
	      writer.flush();
	      writer.close();
		
		
	}

}
